<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "advertisement_list".
 *
 * @property int $id
 * @property int $position_id 广告位id
 * @property int $sort 排序
 * @property string $ad_link 广告链接
 * @property string $ad_title 广告标题
 * @property string $ad_image 广告图片
 * @property int $deleted 是否删除
 * @property string $created
 * @property string $modified
 */
class AdvertisementList extends BaseModel
{
    public function getPrimaryKey($asArray = false)
    {
        return parent::getPrimaryKey($asArray); // TODO: Change the autogenerated stub
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'advertisement_list';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['position_id', 'ad_title', 'ad_image'], 'required'],
            [['position_id', 'sort', 'deleted'], 'integer'],
            [['created', 'modified'], 'safe'],
            [['ad_link'], 'string', 'max' => 100],
            [['ad_title', 'ad_image'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'position_id' => Yii::t('app', '广告位id'),
            'sort' => Yii::t('app', '排序'),
            'ad_link' => Yii::t('app', '广告链接'),
            'ad_title' => Yii::t('app', '广告标题'),
            'ad_image' => Yii::t('app', '广告图片'),
            'deleted' => Yii::t('app', '是否删除'),
            'created' => Yii::t('app', 'Created'),
            'modified' => Yii::t('app', 'Modified'),
        ];
    }

    public function getAdvertisementList($orm = '', $offset, $limit, $forCount = false, $field = 'al.*,ap.position_name,ap.position_key')
    {
        $orm = $this->getOrm($orm)->from('advertisement_list as al')->select($field)
            ->leftJoin('advertisement_position as ap', 'al.position_id = ap.id');
        if ($forCount) {
            $result = $orm->count();
        } else {
            $result = $orm->offset($offset)
                ->limit($limit)
                ->orderBy('ap.id asc,al.id asc')
                ->asArray()
                ->all();
        }
        return $result;
    }
}
